package com.wwj.gulimall.product.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wwj.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wwj.gulimall.product.entity.AttrEntity;
import com.wwj.gulimall.product.service.CategoryService;
import com.wwj.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wwj.gulimall.product.entity.AttrGroupEntity;
import com.wwj.gulimall.product.service.AttrGroupService;
import com.wwj.common.utils.PageUtils;
import com.wwj.common.utils.R;



/**
 * 属性分组
 *
 * @author wwj
 * @email 3288937519@qq.com
 * @date 2024-09-10 16:25:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catId){
        PageUtils page = attrGroupService.queryPageByCatId(params, catId);

        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrGroupService.listAttrByAttrgroupId(attrgroupId);
        return new R().put("data",attrEntities);
    }
    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils page = attrGroupService.getAttrNoRelation(params, attrgroupId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 根据分类id获取分类下所有分组&关联属性
     */
    @RequestMapping("/{catelogId}/withattr")
    public R attrAndAttrGroup(@PathVariable("catelogId") Long catelogId){
        // 先获取到分类下所有分组
        List<AttrGroupEntity> groupEntities = attrGroupService.list(
                new LambdaQueryWrapper<AttrGroupEntity>().
                        eq(AttrGroupEntity::getCatelogId, catelogId));
        // 将数据封装到AttrGroupVo对象中
        List<AttrGroupVo> groupVos = groupEntities.stream().map((item)->{
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);

            // 获取分组关联的属性
            List<AttrEntity> attrEntities =
                    attrGroupService.listAttrByAttrgroupId(item.getAttrGroupId());
            attrGroupVo.setAttrs(attrEntities);
            return attrGroupVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", groupVos);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     *
     * @param
     * @return
     */
    @RequestMapping("/attr/relation")
    public R addAttrRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities){
        attrGroupService.addAttrRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrGroupService.removeRelation(relationEntities);
        return R.ok();
    }

}
